package com.ruipengkj.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TStockAlarm;
import com.ruipengkj.bean.TStore;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.service.TEmployeeService;
import com.ruipengkj.service.TStoreService;

public class StockAlarmModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Vector<List<Object>> rowdatas=new Vector<List<Object>>();
	private Vector<String> columns = new Vector<String>();
	
	private TStoreService tStoreService;
	private TEmployeeService tEmployeeService;
	
	public StockAlarmModel(){
		tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		tStoreService = BuildSpringApplication.getInstance().getTStoreServiceImpl();
		columns.add("报警编号");
		columns.add("商品名称");
		columns.add("报警类型");
		columns.add("仓库名称");
		columns.add("操作人");
		columns.add("操作时间");
		columns.add("最低阀值");
		columns.add("最高阀值");
		columns.add("当前库存量");
	}
	//获得行数
	public int getRowCount(){
		return rowdatas.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		
		return ((Vector)rowdatas.get(row)).get(column);
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//获取列对应的类型
//	public Class getColumnClass(int column){
//		return column == (columns.size()-1) ? ImageIcon.class : Object.class;
//	}
	//设置单元格是否可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	public void removeData(int row){
		rowdatas.remove(row);
	}
	
	
	
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		//取出对应的记录
		Vector rowdata = (Vector)rowdatas.get(rowIndex);
		//删除原来的值
		rowdata.remove(columnIndex);
		//添加用户新修改的值
		rowdata.add(columnIndex,aValue);
	}

	public void setRowData(List<TStockAlarm> tStockAlarmList) {
		rowdatas.clear();
		if(tStockAlarmList!=null&&tStockAlarmList.size()!=0){
			for(int i=0;i<tStockAlarmList.size();i++){
				TStockAlarm tStockAlarm=tStockAlarmList.get(i);
				List<Object> row=new Vector<Object>();
				row.add(tStockAlarm.getAlarmID());//报警编号
				TCommodity commodity=tStockAlarm.getTCommodity();
				if(commodity!=null){
					row.add(commodity.getName());//商品编号
				}else{
					row.add("");//商品编号
				}
				row.add(tStockAlarm.getType());//报警类型
				
				TStore tStore = null;
				try {
					if (tStockAlarm.getStoreID() != null && !tStockAlarm.getStoreID().equals(""))
					tStore = tStoreService.getByIdService(tStockAlarm.getStoreID());
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (tStore != null && tStore.getStoreID() != null) {
					row.add(tStore.getName());//仓库编号
				} else {
					row.add("");
				}
				try {
					if (tStockAlarm.getOperator()!= null&&!tStockAlarm.getOperator().equals("")) {
						TEmployee tEmployee = tEmployeeService.getNameByIdService(tStockAlarm.getOperator());
						if (tEmployee != null && tEmployee.getName() != null){
							row.add(tEmployee.getName());
						}else{
							row.add("");
						}
					} else {
						row.add("");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				row.add(tStockAlarm.getHappenTime());//操作时间
				row.add(tStockAlarm.getMinWalve());//最低阀值
				row.add(tStockAlarm.getMaxWalve());//最高阀值
				row.add(tStockAlarm.getNum());
				rowdatas.add(row);
			}
		}
	}
	
	public void clear(){
		rowdatas.clear();
	}

}
